package com.lhdx.practical.service.impl;

import com.lhdx.practical.bean.Person;
import com.lhdx.practical.dao.PersonDao;
import com.lhdx.practical.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("PersonService")
public class PersonServiceImpl implements PersonService {
    @Autowired
    private PersonDao personDao;

    @Override
    public void save(Person person) {
        personDao.save(person);
    }

    @Override
    public void deleteById(long id) {
        personDao.deleteById(id);
    }

    @Override
    public Person findById(long id) {
        return personDao.findById(id);
    }

    @Override
    public Person findByUsernameAndPassword(String username, String password) {
        return personDao.findByUsernameAndPassword(username,password);
    }

    @Override
    public Page<Person> getResultList(long gradeid,long departid,int type, int page, int pagesize) {
        Sort sort = new Sort(Sort.Direction.DESC, "logintime");
        Pageable pageable = PageRequest.of(page == 0 ? 0 : page -1, pagesize, sort);
        Page<Person> data = personDao.findAll(new Specification<Person>(){
            @Override
            public Predicate toPredicate(Root<Person> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<Predicate>();
                if(gradeid != 0){
                    list.add(cb.equal(root.get("gradeid"),gradeid));
                }
                if(departid != 0){
                    list.add(cb.equal(root.get("departid"),departid));
                }
                if(type != -1){
                    list.add(cb.equal(root.get("type"),type));
                }
                Predicate[] p = new Predicate[list.size()];
                return cb.and(list.toArray(p));
            }
        },pageable);
        return data;
    }

    @Override
    public List<Person> findByDepartid(long departid) {
        return personDao.findByDepartid(departid);
    }

    @Override
    public List<Person> findAllByTypeAndGradeid(int type,long gradeid) {
        return personDao.findAllByTypeAndGradeid(type,gradeid);
    }

    @Override
    public List<Person> findByGroupid(long groupid) {
        return personDao.findByGroupidOrderByJointimeAsc(groupid);
    }

    @Override
    public void updateLoginTime(long id, long logintime) {
        personDao.updateLoginTime(id,logintime);
    }

    @Override
    public void updatePassword(long id, String password) {
        personDao.updatePassword(id,password);
    }

    @Override
    public void updateFaceImg(long id, String imgface) {
        personDao.updateFaceImg(id,imgface);
    }

    @Override
    public void updateIsJoin(long id, int isjoin) {
        personDao.updateIsJoin(id,isjoin);
    }

    @Override
    public void updateGroupid(long id, long groupid,String groupname) {
        personDao.updateGroupid(id,groupid,groupname,new Date().getTime());
    }

    @Override
    public void updateProfile(long id, String profile) {
        personDao.updateProfile(id,profile);
    }

    @Override
    public void updateCancelGroupid(long groupid) {
        personDao.updateCancelGroupid(groupid);
    }

    @Override
    public int countByRealname(String username) {
        return personDao.countByRealname(username);
    }

    @Override
    public int countByUserno(String userno) {
        return personDao.countByUserno(userno);
    }

    @Override
    public int countAllByDepartid(long departid) {
        return personDao.countAllByDepartid(departid);
    }
}
